package sixyz.util;

import java.sql.*;

public class JDBCUtils {

    private static final String url = "jdbc:mysql://localhost:3306/sixyz";
    private static final String userName = "root";
    private static final String password = "root";

    // 取得连接
    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection(url, userName, password);
    }

    // 释放资源
    public void release(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null){
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 重载release方法
    public void release(Statement statement, Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null){
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void release(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
